package com.mvp.shq.washer.ui.recharge;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.RechargeAdapter;
import com.mvp.shq.washer.base.BaseFragment;
import com.mvp.shq.washer.entity.RechargeMoney;
import com.mvp.shq.washer.entity.User;
import com.mvp.shq.washer.rxbus.BusData;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.GridDivider;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.orhanobut.logger.Logger;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class RechargeFragment extends BaseFragment<RechargePresenter, RechargeModel> implements RechargeConstract.View {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.rg_wx)
    RadioButton rgWx;
    @BindView(R.id.arl_wx_pay)
    AutoRelativeLayout arlWxPay;
    @BindView(R.id.rg_alipay)
    RadioButton rgAlipay;
    @BindView(R.id.arl_alipay_pay)
    AutoRelativeLayout arlAlipayPay;
    @BindView(R.id.btn_recharge)
    Button btnRecharge;
    @BindView(R.id.tv_given)
    TextView tvGiven;
    private User user;
    private RechargeAdapter adapter;
    private List<RechargeMoney> moneys;
    private int selectPosition = 0;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_recharge;
    }

    @Override
    public void initView() {
        title.setText("充值");
        back.setVisibility(View.GONE);
        recyclerview.setLayoutManager(new GridLayoutManager(context,3,LinearLayoutManager.VERTICAL,false));
        RecyclerViewDivider divider1 = new RecyclerViewDivider(context,LinearLayoutManager.VERTICAL,28,getResources().getColor(R.color.white));
        recyclerview.addItemDecoration(divider1);
        rgWx.setChecked(true);
    }

    private void initUser(){
        user = SpUtil.getUser();
        if (user != null){
            tvBalance.setText(user.getRechargeBalance()+"");
        }
    }

    @OnClick({R.id.arl_wx_pay, R.id.arl_alipay_pay, R.id.btn_recharge,R.id.rg_alipay,R.id.rg_wx})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.arl_wx_pay:
                rgWx.setChecked(true);
                rgAlipay.setChecked(false);
                break;
            case R.id.arl_alipay_pay:
                rgWx.setChecked(false);
                rgAlipay.setChecked(true);
                break;
            case R.id.rg_alipay:
                rgWx.setChecked(false);
                rgAlipay.setChecked(true);
                break;
            case R.id.rg_wx:
                rgWx.setChecked(true);
                rgAlipay.setChecked(false);
                break;
            case R.id.btn_recharge:
                break;
        }
    }
    @Subscribe(code = EventCode.ON_RECHARGEMONEY_CLICK)
    public void onItemClick(BusData data){
        int position = data.getPosition();
        if (position == selectPosition){
            return;
        }
        moneys.get(selectPosition).setSelect(false);
        moneys.get(position).setSelect(true);
        selectPosition = position;
        adapter.notifyDataSetChanged();
        setGivenTV();
    }

    @Override
    public void getRecharMoneySuccess(List<RechargeMoney> list) {
        if (list == null){
            return;
        }
        moneys = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            list.get(i).setSelect(false);
            moneys.add(list.get(i));
        }
        selectPosition = 0;
        if (moneys.size() >= 1){
            moneys.get(selectPosition).setSelect(true);
        }
        setGivenTV();
        adapter = new RechargeAdapter(context,moneys);
        recyclerview.setAdapter(adapter);
    }
    public void setGivenTV(){
        String str = "赠送";
        for (int i=0;i<moneys.get(selectPosition).getCoupons().size();i++){
            str += moneys.get(selectPosition).getCoupons().get(i).getCouponName();
            str += "x ";
            str += moneys.get(selectPosition).getCoupons().get(i).getCouponCount();
            if (i != moneys.get(selectPosition).getCoupons().size()-1){
                str += "、";
            }
        }
        tvGiven.setText(str);
    }
    @Override
    public void getRecharMoneyFail(String msg) {
        T.show("获取充值金额失败");
    }
}
